package org.elip.stewiemaze.server.utils;

import java.io.UnsupportedEncodingException;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class MailUtils {
	
	public static void sendValidationMail(String email, String nickname) throws MessagingException, UnsupportedEncodingException {
		Properties props = new Properties();
		Session mailSession = Session.getInstance(props, null);
        String msgBody = "Click" + "<a href='http://stewiemaze.appspot.com/register-player.do/validate.do?nickname=" + nickname +"'>" +
        		" Here</a> to validate you account";
        Message msg = new MimeMessage(mailSession);
        Multipart mp = new MimeMultipart();
        MimeBodyPart htmlPart = new MimeBodyPart();
        htmlPart.setContent(msgBody, "text/html");
        mp.addBodyPart(htmlPart);
        msg.setContent(mp);
        msg.setFrom(new InternetAddress("admin@stewiemaze.appspotmail.com"));
        msg.addRecipient(Message.RecipientType.TO,
                         new InternetAddress(email, nickname));
        msg.setSubject("Account Activation");
        Transport.send(msg);
	}

	public static void sendNotification(String email, String nickname,
			Long newScore) throws MessagingException, UnsupportedEncodingException {
		Properties props = new Properties();
		Session session = Session.getDefaultInstance(props, null);

		String msgBody = "Your record was beaten by " + nickname + ", who scored " + newScore;

		Message msg = new MimeMessage(session);
		msg.setFrom(new InternetAddress("admin@stewiemaze.appspotmail.com"));
		msg.addRecipient(Message.RecipientType.TO,
				new InternetAddress(email));
		msg.setSubject("Record beaten");
		msg.setText(msgBody);
		Transport.send(msg);
	}
}
